home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / FixStickies / FixStickies.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-24  |  5.2 KB  |  224 lines  |  [TEXT/SPM ]

  1. // FixStickies.c
  2. // Fix the message that Twits use when they leave their machines idle in the MacHack
  3. // machine room with a "Do Not Use" message open on them.
  4. //
  5. // 23 Jun 95 Mike Crawford crawford@scruznet.com
  6. // Look for updates, and my real hack, via anonymous ftp from ftp.scruznet.com, in
  7. // the directory /users/crawford/public/MacFiles
  8. //
  9. // Copyright ©1995 Free Software Foundation.  See the file COPYING for details on the license
  10. // to copy this program and its source code
  11. //
  12. // Revision History (most recent first)
  13. //
  14. // 23 Jun 95    MDC    New today
  15.  
  16. #include <SetUpA4.h>
  17. #include <Traps.h>
  18.  
  19. // State machine values
  20. #define kIdle     0
  21. #define kCmdKey 2
  22. #define kTyping    4
  23.  
  24. #define Ticks    0x16a
  25. #define Mouse    0x830
  26. #define CurApName    0x910
  27.  
  28. #define FAST_TEST
  29.  
  30. #ifdef FAST_TEST
  31. //#define kIdleTime    0x12c
  32. #define kIdleTime    0x708
  33. #else
  34. #define kIdleTime    0xd2f0
  35. #endif
  36.  
  37. #define kMsgLen        0x24
  38.  
  39. pascal void main( void )
  40. {
  41.  
  42.     asm{
  43. ;        Debugger                            ; Uncomment this for debugging; set PC to the rts to avoid load
  44.         RecoverHandle
  45.         HLock
  46.         Move.l    a0, -(sp)
  47.         DetachResource
  48.         Move.l    #0xA970, d0                    ; GetNextEvent trap word
  49.         GetToolTrapAddress
  50.         Move.l    a0, d0
  51.         Lea        @realTrapAddress, a0        ; Can't write to a PC relative offset.  Can't imagine why!
  52.         Move.l    d0, (a0)
  53.         Move.l    #0x4, a1                    
  54.         Moveq    #0x9, d0                    ; Hey, Symantec, update your headers!
  55.         dc.w    0xA098                        ; _HWPriv FlushCodeCacheRange( @realTrapAddress, 4 )
  56.                                             ; Flushing whole cache under DR emulator blows out 250k; don't
  57.                                             ; do it - flush ranges instead
  58.         lea        @GetNextEventPatch, a0
  59.         Move.l    #0xA970, d0
  60.         SetToolTrapAddress
  61.         rts
  62.         
  63.         ; Data storage
  64.  
  65. @lastTicks
  66.         dc.l    0x0fffffff
  67.         
  68. @lastMouse
  69.         dc.l    0x00000000    
  70.  
  71. @saveRts
  72.         dc.l    0x00000000
  73.         
  74. @evtPtr
  75.         dc.l    0x00000000
  76.  
  77. @charactersTyped
  78.         dc.w    0x0000
  79.  
  80. @stateMachine
  81.         dc.w    kIdle
  82.  
  83. @GetNextEventPatch
  84.         Lea        @saveRts, a0                ; Stash real return address
  85.         Move.l    (sp)+, (a0)
  86.  
  87.         ; (sp) is the event record pointer
  88.         ; 4(sp) is the eventMask
  89.         ; 6(sp) will be the result
  90.         
  91.         Lea        @evtPtr, a0
  92.         Move.l    (sp), (a0)                    ; Save pointer to event record
  93.                                             ; We must do this because GetNextEvent will remove params from stack
  94.         
  95.         dc.w    0x4eb9                        ; First word of a JSR to long absolute address
  96. @realTrapAddress
  97.         dc.l    0x00000000                    ; JSR to this address to call real GetNextEvent
  98.         
  99.         ; Now we do a switch off of the current state machine value to either
  100.         ; look at the result of GetNextEvent, or fudge the contents of the EventRecord
  101.         
  102.         Move.w    @stateMachine, d0
  103.         Lea        @jumpTable, a0
  104.         Adda.w    d0, a0
  105.         Jmp        (a0)
  106.  
  107. @jumpTable
  108.         Bra        @doIdle
  109.         Bra        @doCmdKey
  110.         Bra        @doTyping
  111.  
  112. @doIdle
  113.         ; First we check to see if there is anything but a null event in the EventRecord
  114.         ; extern pascal Boolean GetNextEvent(short eventMask, EventRecord *theEvent)
  115.         
  116.         Move.l    @evtPtr, a0
  117.         Tst.w    (a0)                        ; if ( eventPtr->what == nullEvt )
  118.         Beq        @isNullEvent
  119.         
  120.         ; In this case we have a real, non-null event.  Save the time and mouse
  121.         Lea        @lastTicks, a0
  122.         Move.l    Ticks, (a0)
  123.         Lea        @lastMouse, a0
  124.         Move.l    Mouse, (a0)
  125.         Bra        @doReturn    
  126.         
  127. @isNullEvent
  128.  
  129.         ; We're idle.  Has the mouse moved?
  130.         Lea        @lastMouse, a0
  131.         Move.l    Mouse, d0
  132.         Cmp.l    (a0), d0
  133.         Bne        @mouseMoved
  134.         
  135.         ; Has enough time gone by?
  136.         
  137.         Lea        @lastTicks, a0
  138.         Move.l    (a0), d0
  139.         Add.l    #kIdleTime, d0
  140.         Lea        Ticks, a0
  141.         Cmp.l    (a0), d0
  142.         Bgt        @doReturn
  143.         
  144. @timeGoneBy
  145.         ; Are we in Stickies?  The proper way would be to get the process info and look at the creator
  146.         ; But the hack contest has already started
  147.         
  148.         Lea        CurApName, a0
  149.         Cmp.l    #0x08537469, (a0)            ; First four bytes of "\pStickies"
  150.         Bne        @doReturn
  151.         
  152.         ; Time to get to work
  153.         Lea        @stateMachine, a0
  154.         Move.w    #kCmdKey, (a0)                ; Next time around we'll do a Select All
  155.         Bra        @doReturn
  156.         
  157. @mouseMoved
  158.         Lea        @lastMouse, a0
  159.         Move.l    Mouse, (a0)        
  160.         Bra        @doReturn
  161.  
  162. @doCmdKey
  163.         ; Here we make the event record into a Command-A
  164.         Move.l    @evtPtr, a0
  165.         Move.w    #keyDown, (a0)                ; evtPtr->what = keyDown
  166.         Move.l    #0x00020061, 2(a0)            ; evtPtr->message = letter 'A'
  167.         Move.w    #0x0180, 0xe(a0)            ; Command-A is Select all
  168.         Move.w    #0x0100, (sp)                ; Set GNE result code to "true"
  169.  
  170.         Lea        @stateMachine, a0
  171.         Move.w    #kTyping, (a0)                ; Next time around we'll start typing
  172.         Lea        @charactersTyped, a0        ; Reset keystroke counter
  173.         Clr.w    (a0)
  174.     
  175.         Bra        @doReturn
  176.  
  177. @doTyping
  178.         ; Here we make the event record into a typed character
  179.         Move.w    @charactersTyped, d0
  180.         Cmp.w    #kMsgLen, d0
  181.         Bge        @doneTyping
  182.         
  183.         Lea        @charactersTyped, a0
  184.         Add.w    #1, (a0)                    ; Increment counter
  185.         Lea        @messageStr, a0
  186.         Adda.w    d0, a0
  187.         Moveq    #0, d0
  188.         Move.b    (a0), d0                    ; fetch a character
  189.             
  190.         Move.l    @evtPtr, a0
  191.         Move.w    #keyDown, (a0)                ; evtPtr->what = keyDown
  192.         Move.l    d0, 2(a0)                    ; evtPtr->message = next letter (note we have no key code)
  193.         Move.w    #0x0000, 0xe(a0)            ; No modifiers
  194.         Move.w    #0x0100, (sp)                ; Set GNE result code to "true"
  195.         
  196.         Bra        @doReturn
  197.  
  198. @doneTyping
  199.         Lea        @stateMachine, a0
  200.         Move.w    #kIdle, (a0)                ; Next time around we'll start typing
  201.         Lea        @lastTicks, a0                ; Reset timer
  202.         Move.l    Ticks, (a0)
  203.         
  204. @doReturn
  205.         Move.l    @saveRts, a0
  206.         Jmp        (a0)                        ; Return to real caller
  207. @messageStr
  208.         dc.l    'Plea'
  209.         dc.l    'se f'
  210.         dc.l    'eel '
  211.         dc.l    'free'
  212.         dc.l    ' to '
  213.         dc.l    'use '
  214.         dc.l    'this'
  215.         dc.l    ' mac'
  216.         dc.l    'hine'            
  217.     }
  218.     
  219.     
  220.         
  221.     return;
  222. }
  223.  
  224.